//package com.kly.sc.api.application.consumer;
//
//import com.kly.sc.api.application.constant.ApiRedisKeys;
//import com.kly.sc.api.application.service.utils.RedisService;
//import com.kly.user.enums.OnlineStatus;
//import com.kly.user.service.UserService;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//
//import javax.annotation.PostConstruct;
//import java.util.Set;
//
//@Component
//public class ChatUserOnlineConsumer {
//
//    private final Logger logger = LoggerFactory.getLogger(getClass());
//
//    @Autowired
//    RedisService redisService;
//
//    @Autowired
//    UserService userService;
//
//    private static final long expireInSeconds = 10L;
//
//    private static Thread onLineThread;
//
//    @PostConstruct
//    private void offlineConsumer() {
//        Thread thread = new Thread(() -> {
//            while (true) {
//                try {
//                    if (redisService.setNX(ApiRedisKeys.CHAT_USER_ONLINE_LOCK.getValue(), "userOnlineLock",
//                            expireInSeconds, "userOnlineLock")) {
//                        Set<String> tasks = redisService.getDelayTask(
//                                ApiRedisKeys.CHAT_USER_ONLINE_QUEUE.getValue(), "userOnlineQueue");
//                        //从有序集合中获取最早到达预定时间的任务
//                        if (tasks != null && !tasks.isEmpty()) {
//                            logger.info("chat用户定时下线: "+tasks+"\n 当前时间："+System.currentTimeMillis()/1000);
//                            offlineChat(tasks);
//                            redisService.delDelayTask(
//                                    ApiRedisKeys.CHAT_USER_ONLINE_QUEUE.getValue(), tasks, "userOnlineQueue");
//                        }
//                    }
//
//                } catch (Exception e) {
//                    logger.error("An error occurred: ", e);
//                } finally {
//                    try {
//                        redisService.del(ApiRedisKeys.CHAT_USER_ONLINE_LOCK.getValue(), "userOnlineLock");
//                        Thread.sleep(2000);
//                    } catch (InterruptedException ex) {
//                        ex.printStackTrace();
//                        break;
//                    }
//                }
//            }
//        });
//        thread.start();
//
//        onLineThread=thread;
//
//    }
//
//    public void interrupt(){
//        if (onLineThread!=null){
//            onLineThread.interrupt();
//        }
//    }
//
//    void offlineChat(Set<String> tasks){
//        logger.info("offlineChat set: " + tasks + " current time：" + System.currentTimeMillis() / 1000);
//        for (String userCode:tasks){
//            try {
//                userService.saveOrUpdateChatOnlineRecord(userCode, 2);
//            }catch (Exception e){
//                logger.error("offlineChat error : "+userCode, e);
//            }
//        }
//
//    }
//
//}
